/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frame.TaiKhoan;

import gui.customcbb;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import myclass.MyAdminFrame;
import pojo.Nhanvien;

/**
 *
 * @author Cong
 */
public class QLKhachHang extends MyAdminFrame {

    /**
     * Creates new form QLKhachHang
     */
    private static final String columns[] = {"ID", "Tên khách hàng", "Ngày sinh", "Giới tính", "Email", "Địa chỉ", "Điện thoại", "Ngày lập tài khoản", "Nhân viên lập"};
    private DefaultTableModel tbModelkhachhang;
    private DefaultComboBoxModel cboxModelgioitinh;
    private static SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yyyy");

    public QLKhachHang(Nhanvien nv) {
        super(nv);
        initComponents();

        tbModelkhachhang = new DefaultTableModel();
        tbModelkhachhang.setColumnIdentifiers(columns);
        cboxModelgioitinh = new DefaultComboBoxModel();
        jtbkhachhang.setModel(tbModelkhachhang);
        jcbbgioitinh.setModel(cboxModelgioitinh);
        LoadCBB();
        LoadTB();
    }

    private void LoadCBB() {
        cboxModelgioitinh.removeAllElements();
        cboxModelgioitinh.addElement(new customcbb(1, "Nam"));
        cboxModelgioitinh.addElement(new customcbb(2, "Nữ"));
    }

    private void LoadTB() {
        tbModelkhachhang.getDataVector().clear();
        List<pojo.Khachhang> ds = dao.daoKhachHang.LaydsKhachHang();
        if (!ds.isEmpty()) {
            for (int i = 0; i < ds.size(); i++) {
                pojo.Khachhang kh = ds.get(i);
                tbModelkhachhang.addRow(new Object[]{kh.getId(), kh.getTenkhachhang(),
                            (kh.getNgaysinh() == null) ? "" : fm.format(kh.getNgaysinh()), getSex(kh.getGioitinh()), kh.getEmail(),
                            kh.getDiachi(), kh.getDienthoai(), fm.format(kh.getNgaytaotaikhoan()),
                            kh.getNhanvien().getHoten()});
            }
        }
    }

    private String getSex(String sex) {
        String s = "Nam";
        if ("F".equals(sex)) {
            s = "Nữ";
        }
        return s;
    }

    private String getSexKey(String sex) {
        String s = "M";
        if ("Nữ".equals(sex)) {
            s = "F";
        }
        return s;
    }

    private int getSexKeyIndex(String sex) {
        int s = 1;
        if ("Nữ".equals(sex)) {
            s = 2;
        }
        return s;
    }

    private void ClearDT() {
        jtfdiachi.setText("");
        jtfdienthoai.setText("");
        jtfemail.setText("");
        jtfngaysinh.setText("");
        jtfngaytao.setText("");
        jtfnguoitao.setText("");
        jtftenkhachhang.setText("");
        jlbmakhachhang.setText("");
        jcbbgioitinh.setSelectedIndex(0);
    }

    private boolean CheckField() {
        if ("".equals(jtftenkhachhang.getText())) {
            JOptionPane.showMessageDialog(this, "Chưa nhập tên khách hàng.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if (!"".equals(jtfngaytao.getText())) {
            try {
                fm.parse(jtfngaytao.getText());
            } catch (ParseException pe) {
                JOptionPane.showMessageDialog(this, "Ngày tạo không hợp lệ.", "Thông báo", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }
        if (!"".equals(jtfnguoitao.getText())) {
            pojo.Nhanvien nv = new pojo.Nhanvien();
            nv.setHoten(jtfnguoitao.getText());
            List<pojo.Nhanvien> ds = dao.daoKhachHang.GetNV(nv);
            if (ds.isEmpty()) {
                JOptionPane.showMessageDialog(this, "Nhân viên không tồn tại.", "Thông báo", JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }
        try {
            Long.parseLong(jtfdienthoai.getText());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Số điện thoại không hợp lệ.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbkhachhang = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jlbmakhachhang = new javax.swing.JLabel();
        jtftenkhachhang = new javax.swing.JTextField();
        jtfngaysinh = new javax.swing.JTextField();
        jcbbgioitinh = new javax.swing.JComboBox();
        jtfemail = new javax.swing.JTextField();
        jtfdiachi = new javax.swing.JTextField();
        jtfngaytao = new javax.swing.JTextField();
        jtfdienthoai = new javax.swing.JTextField();
        jtfnguoitao = new javax.swing.JTextField();
        jbttimkiem = new javax.swing.JButton();
        jbtreset = new javax.swing.JButton();
        jbtthoat = new javax.swing.JButton();
        jbtluu = new javax.swing.JButton();
        jbtxoa = new javax.swing.JButton();
        jtbthem = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Quản lý khách hàng");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Quản lý khách hàng");

        jtbkhachhang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtbkhachhang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbkhachhangMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtbkhachhang);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin khách hàng"));

        jLabel2.setText("Mã khách hàng:");

        jLabel3.setText("Tên khách hàng:");

        jLabel4.setText("Ngày sinh:");

        jLabel5.setText("Giới tính:");

        jLabel6.setText("Email:");

        jLabel7.setText("Địa chỉ:");

        jLabel8.setText("Điện thoại:");

        jLabel9.setText("Ngày tạo tài khoản:");

        jLabel10.setText("Người tạo:");

        jcbbgioitinh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jbttimkiem.setText("Tìm kiếm");
        jbttimkiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbttimkiemActionPerformed(evt);
            }
        });

        jbtreset.setText("Nhập lại");
        jbtreset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtresetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel6)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jlbmakhachhang, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcbbgioitinh, javax.swing.GroupLayout.Alignment.LEADING, 0, 324, Short.MAX_VALUE)
                    .addComponent(jtfngaysinh, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtftenkhachhang, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfemail, javax.swing.GroupLayout.Alignment.LEADING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel10))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jtfdienthoai)
                        .addComponent(jtfngaytao)
                        .addComponent(jtfnguoitao)
                        .addComponent(jtfdiachi, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jbttimkiem, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jbtreset, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jtfdiachi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jlbmakhachhang))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jtfdienthoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(jtfngaytao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(jtfnguoitao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbttimkiem)
                            .addComponent(jbtreset)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtftenkhachhang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtfngaysinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jcbbgioitinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtfemail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jbtthoat.setText("Thoát");
        jbtthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtthoatActionPerformed(evt);
            }
        });

        jbtluu.setText("Lưu");
        jbtluu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtluuActionPerformed(evt);
            }
        });

        jbtxoa.setText("Xóa");
        jbtxoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtxoaActionPerformed(evt);
            }
        });

        jtbthem.setText("Thêm");
        jtbthem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtbthemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(390, 390, 390)
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jtbthem, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtxoa, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtluu, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtthoat, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(15, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtthoat)
                    .addComponent(jbtluu)
                    .addComponent(jbtxoa)
                    .addComponent(jtbthem))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtbkhachhangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbkhachhangMouseClicked
        int dong = jtbkhachhang.getSelectedRow();
        jlbmakhachhang.setText(jtbkhachhang.getValueAt(dong, 0).toString());
        jtftenkhachhang.setText(jtbkhachhang.getValueAt(dong, 1).toString());
        jtfngaysinh.setText(jtbkhachhang.getValueAt(dong, 2).toString());
        jcbbgioitinh.setSelectedIndex(getSexKeyIndex(jtbkhachhang.getValueAt(dong, 3).toString()) - 1);
        jtfemail.setText(jtbkhachhang.getValueAt(dong, 4).toString());
        jtfdiachi.setText(jtbkhachhang.getValueAt(dong, 5).toString());
        jtfdienthoai.setText(jtbkhachhang.getValueAt(dong, 6).toString());
        jtfngaytao.setText(jtbkhachhang.getValueAt(dong, 7).toString());
        jtfnguoitao.setText(jtbkhachhang.getValueAt(dong, 8).toString());
    }//GEN-LAST:event_jtbkhachhangMouseClicked

    private void jbttimkiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbttimkiemActionPerformed
        pojo.Khachhang kh = new pojo.Khachhang();
        kh.setDiachi(jtfdiachi.getText());
        kh.setDienthoai(jtfdienthoai.getText());
        kh.setEmail(jtfemail.getText());
        kh.setGioitinh(getSexKey(jcbbgioitinh.getSelectedItem().toString()));
        if (!"".equals(jtfngaysinh.getText())) {
            try {
                kh.setNgaysinh(fm.parse(jtfngaysinh.getText()));
            } catch (ParseException ex) {
                Logger.getLogger(QLKhachHang.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            kh.setNgaysinh(null);
        }
        kh.setTenkhachhang(jtftenkhachhang.getText());
        if (!"".equals(jtfngaytao.getText())) {
            try {
                kh.setNgaytaotaikhoan(fm.parse(jtfngaytao.getText()));
            } catch (ParseException ex) {
                Logger.getLogger(QLKhachHang.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            kh.setNgaytaotaikhoan(null);
        }
        pojo.Nhanvien nv = new pojo.Nhanvien();
        if (!"".equals(jtfnguoitao.getText())) {
            nv.setHoten(jtfnguoitao.getText());
            List<pojo.Nhanvien> ds = dao.daoKhachHang.GetNV(nv);
            kh.setNhanvien(ds.get(0));
        } else {
            kh.setNhanvien(null);
        }
        List<pojo.Khachhang> ds = dao.daoKhachHang.FindKH(kh);
        if (!ds.isEmpty()) {
            tbModelkhachhang.getDataVector().clear();
            for (int i = 0; i < ds.size(); i++) {
                pojo.Khachhang kh1 = ds.get(i);
                tbModelkhachhang.addRow(new Object[]{kh1.getId(), kh1.getTenkhachhang(),
                            (kh1.getNgaysinh() == null) ? "" : fm.format(kh1.getNgaysinh()), getSex(kh1.getGioitinh()), kh1.getEmail(),
                            kh1.getDiachi(), kh1.getDienthoai(), fm.format(kh1.getNgaytaotaikhoan()),
                            kh1.getNhanvien().getHoten()});
            }
        } else {
            JOptionPane.showMessageDialog(this, "Không tìm thấy.", "Thông báo", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbttimkiemActionPerformed

    private void jbtresetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtresetActionPerformed
        ClearDT();
    }//GEN-LAST:event_jbtresetActionPerformed

    private void jbtthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtthoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbtthoatActionPerformed

    private void jbtluuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtluuActionPerformed
        if ("".equals(jlbmakhachhang.getText())) {
            JOptionPane.showMessageDialog(this, "Chưa chọn khách hàng cần sửa.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (CheckField()) {
            pojo.Khachhang kh = new pojo.Khachhang();
            kh.setId(Long.parseLong(jlbmakhachhang.getText()));
            kh.setDiachi(jtfdiachi.getText());
            kh.setDienthoai(jtfdienthoai.getText());
            kh.setEmail(jtfemail.getText());
            kh.setGioitinh(getSexKey(jcbbgioitinh.getSelectedItem().toString()));
            if (!"".equals(jtfngaysinh.getText())) {
                try {
                    kh.setNgaysinh(fm.parse(jtfngaysinh.getText()));
                } catch (ParseException ex) {
                    Logger.getLogger(QLKhachHang.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            kh.setTenkhachhang(jtftenkhachhang.getText());
            if (!"".equals(jtfngaytao.getText())) {
                try {
                    kh.setNgaytaotaikhoan(fm.parse(jtfngaytao.getText()));
                } catch (ParseException ex) {
                    Logger.getLogger(QLKhachHang.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                kh.setNgaytaotaikhoan(Calendar.getInstance().getTime());
            }
            pojo.Nhanvien nv = new pojo.Nhanvien();
            if (!"".equals(jtfnguoitao.getText())) {
                nv.setHoten(jtfnguoitao.getText());
            } else {
                nv.setHoten("Võ Chí Công");
            }
            List<pojo.Nhanvien> ds = dao.daoKhachHang.GetNV(nv);
            kh.setNhanvien(ds.get(0));
            boolean kq = dao.daoKhachHang.LuuKH(kh);
            if (kq == true) {
                LoadTB();
            } else {
                JOptionPane.showMessageDialog(this, "Cập nhật thất bại.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jbtluuActionPerformed

    private void jbtxoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtxoaActionPerformed
        if ("".equals(jlbmakhachhang.getText())) {
            JOptionPane.showMessageDialog(this, "Chưa chọn khách hàng cần xóa.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (JOptionPane.showConfirmDialog(this, "Bạn có chắc muốn xóa không?", "Xác nhận", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            pojo.Khachhang kh = new pojo.Khachhang();
            kh.setId(Long.parseLong(jlbmakhachhang.getText()));
            boolean kq = dao.daoKhachHang.XoaKH(kh);
            if (kq == true) {
                LoadTB();
            } else {
                JOptionPane.showMessageDialog(this, "Xóa thất bại.\nXin hãy kiểm tra lại ràng buộc dữ liệu.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jbtxoaActionPerformed

    private void jtbthemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtbthemActionPerformed
        if (CheckField()) {
            pojo.Khachhang kh = new pojo.Khachhang();
            kh.setDiachi(jtfdiachi.getText());
            kh.setDienthoai(jtfdienthoai.getText());
            kh.setEmail(jtfemail.getText());
            kh.setGioitinh(getSexKey(jcbbgioitinh.getSelectedItem().toString()));
            if (!"".equals(jtfngaysinh.getText())) {
                try {
                    kh.setNgaysinh(fm.parse(jtfngaysinh.getText()));
                } catch (ParseException ex) {
                    Logger.getLogger(QLKhachHang.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            kh.setTenkhachhang(jtftenkhachhang.getText());
            if (!"".equals(jtfngaytao.getText())) {
                try {
                    kh.setNgaytaotaikhoan(fm.parse(jtfngaytao.getText()));
                } catch (ParseException ex) {
                    Logger.getLogger(QLKhachHang.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                kh.setNgaytaotaikhoan(Calendar.getInstance().getTime());
            }
            pojo.Nhanvien nv = new pojo.Nhanvien();
            if (!"".equals(jtfnguoitao.getText())) {
                nv.setHoten(jtfnguoitao.getText());
            } else {
                nv.setHoten("Võ Chí Công");
            }
            List<pojo.Nhanvien> ds = dao.daoKhachHang.GetNV(nv);
            kh.setNhanvien(ds.get(0));
            boolean kq = dao.daoKhachHang.ThemKhachHang(kh);
            if (kq == true) {
                LoadTB();
            } else {
                JOptionPane.showMessageDialog(this, "Thêm thất bại.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jtbthemActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtluu;
    private javax.swing.JButton jbtreset;
    private javax.swing.JButton jbtthoat;
    private javax.swing.JButton jbttimkiem;
    private javax.swing.JButton jbtxoa;
    private javax.swing.JComboBox jcbbgioitinh;
    private javax.swing.JLabel jlbmakhachhang;
    private javax.swing.JTable jtbkhachhang;
    private javax.swing.JButton jtbthem;
    private javax.swing.JTextField jtfdiachi;
    private javax.swing.JTextField jtfdienthoai;
    private javax.swing.JTextField jtfemail;
    private javax.swing.JTextField jtfngaysinh;
    private javax.swing.JTextField jtfngaytao;
    private javax.swing.JTextField jtfnguoitao;
    private javax.swing.JTextField jtftenkhachhang;
    // End of variables declaration//GEN-END:variables
}
